using ScriptCoreLib;
using ScriptCoreLib.Delegates;
using ScriptCoreLib.Extensions;
using ScriptCoreLib.JavaScript;
using ScriptCoreLib.JavaScript.Components;
using ScriptCoreLib.JavaScript.DOM;
using ScriptCoreLib.JavaScript.DOM.HTML;
using ScriptCoreLib.JavaScript.Extensions;
using System;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using LegacyWarCraft.HTML.Pages;
using LegacyWarCraft.HTML.Images.FromAssets;

namespace LegacyWarCraft
{
    /// <summary>
    /// This type will run as JavaScript.
    /// </summary>
    internal sealed class Application
    {
        //http://jsc.sourceforge.net/examples/legacy/war/ Source form Web;

        public readonly ApplicationWebService service = new ApplicationWebService();

        /// <summary>
        /// This is a javascript application.
        /// </summary>
        /// <param name="page">HTML document rendered by the web server which can now be enhanced.</param>
        public Application(IDefaultPage page)
        {

            var width = 32;
            Action<int, int, int, int> CreateGrid = 
            (gridLeftPosition, gridTopPosition, cols, rows) => 
            {
              
                for (int x = 0; x < cols; x++)
                {
                    for (int y = 0; y < rows; y++)
                    {
                        new grass().AttachToDocument().style.SetLocation(
                        left: gridLeftPosition + x * width,
                        top: gridTopPosition + y * width);

                    }


                }
            };
            CreateGrid(100, 100, 9, 9);
            CreateGrid(500, 100, 9, 9);
            CreateGrid(900, 100, 9, 9);
            
            CreateGrid(100, 500, 9, 9);
            CreateGrid(100, 900, 9, 9);
            CreateGrid(500, 900, 9, 9);

            CreateGrid(900, 500, 9, 9);
            CreateGrid(900, 900, 9, 9);




            {
                var gridLeftPosition = 500;
                var gridTopPosition = 500;
                for (int x = 0; x < 10; x++)
                {
                    for (int y = 0; y < 10; y++)
                    {
                        var isleft = x == 0;
                        var istop = y == 0;
                        var isright = x == 9;
                        var isbottom = y == 9;
                        var isleftbottom = isleft && isbottom;
                        var islefttop = isleft && istop;
                        var isrighttop = isright && istop;
                        var isrightbottom = isright && isbottom;
                        var iscorner = islefttop || isleftbottom || isrightbottom || isrighttop;
                        var isedge = isleft || isright || istop || isbottom;


                        if (iscorner)
                        {
                            if (islefttop)
                            {
                                new forest_lt().AttachToDocument().style.SetLocation(
                                left: gridLeftPosition + x * width,
                                top: gridTopPosition + y * width);
                            }
                            else if (isleftbottom)
                            {
                                new forest_lb().AttachToDocument().style.SetLocation(
                                left: gridLeftPosition + x * width,
                                top: gridTopPosition + y * width);
                            }
                            else if (isrighttop)
                            {
                                new forest_rt().AttachToDocument().style.SetLocation(
                                left: gridLeftPosition + x * width,
                                top: gridTopPosition + y * width);
                            }
                            else

                                new forest_rb().AttachToDocument().style.SetLocation(
                                left: gridLeftPosition + x * width,
                                top: gridTopPosition + y * width);




                        }

                        else if (isedge)
                        {
                            if (isbottom)
                            {
                                new forest_b().AttachToDocument().style.SetLocation(
                                left: gridLeftPosition + x * width,
                                top: gridTopPosition + y * width);
                            }
                            else if (istop)
                            {
                                new forest_t().AttachToDocument().style.SetLocation(
                                left: gridLeftPosition + x * width,
                                top: gridTopPosition + y * width);
                            }
                            else if (isleft)
                            {
                                new forest_l().AttachToDocument().style.SetLocation(
                                left: gridLeftPosition + x * width,
                                top: gridTopPosition + y * width);
                            }
                            else
                                new forest_r().AttachToDocument().style.SetLocation(
                                left: gridLeftPosition + x * width,
                                top: gridTopPosition + y * width);
                        }

                        else
                        {
                            new forest_x().AttachToDocument().style.SetLocation(
                            left: gridLeftPosition + x * width,
                            top: gridTopPosition + y * width);
                        }


                    }


                }
            }

          

            @"Hello world".ToDocumentTitle();
            // Send data from JavaScript to the server tier
            service.WebMethod2(
                @"A string from JavaScript.",
                value => value.ToDocumentTitle()
            );
        }

    }
}
